package com.example.lottery_system.controller;

import com.example.lottery_system.common.errorcode.ControllerErrorCodeConstants;
import com.example.lottery_system.common.exception.ControllerException;
import com.example.lottery_system.common.pojo.CommonResult;
import com.example.lottery_system.common.utiils.JacksonUtil;
import com.example.lottery_system.controller.param.ShortMessageLoginParam;
import com.example.lottery_system.controller.param.UserPasswordLoginParam;
import com.example.lottery_system.controller.param.UserRegisterParam;
import com.example.lottery_system.controller.result.UserLoginResult;
import com.example.lottery_system.controller.result.UserRegisterResult;
import com.example.lottery_system.service.dto.UserLoginDTO;
import com.example.lottery_system.service.impl.UserServiceImpl;
import com.example.lottery_system.service.dto.UserRegisterDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/register")
    public CommonResult<UserRegisterResult> register(@RequestBody UserRegisterParam userRegisterParam) {
        log.info("userRegister UserRegisterParam:{}", JacksonUtil.writeValueAsString(userRegisterParam));
        UserRegisterDTO userRegisterDTO = userService.register(userRegisterParam);
        return CommonResult.success(convertToUserRegisterResult(userRegisterDTO));
    }

    private UserRegisterResult convertToUserRegisterResult(UserRegisterDTO userRegisterDTO) {
        UserRegisterResult userRegisterResult = new UserRegisterResult();
        if (null == userRegisterDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }
        userRegisterResult.setId(userRegisterDTO.getId());
        return userRegisterResult;
    }

    @RequestMapping("/password/login")
    public CommonResult<UserLoginResult> userLogin(@Validated @RequestBody UserPasswordLoginParam userPasswordLoginParam) {
        log.info("userLogin userPasswordParam:" + JacksonUtil.writeValueAsString(userPasswordLoginParam));
        UserLoginDTO userLoginDTO = userService.login(userPasswordLoginParam);
        return CommonResult.success(convertToUserLoginResult(userLoginDTO));
    }

    @RequestMapping("/shortMessage/login")
    public CommonResult<UserLoginResult> shortMessageLogin(@Validated @RequestBody ShortMessageLoginParam shortMessageLoginParam) {
        log.info("userLogin shortMessageParam:" + JacksonUtil.writeValueAsString(shortMessageLoginParam));
        UserLoginDTO userLoginDTO = userService.login(shortMessageLoginParam);
        return CommonResult.success(convertToUserLoginResult(userLoginDTO));
    }

    private UserLoginResult convertToUserLoginResult(UserLoginDTO userLoginDTO) {
        UserLoginResult userLoginResult = new UserLoginResult();
        userLoginResult.setToken(userLoginDTO.getToken());
        userLoginResult.setIdentity(userLoginResult.getIdentity());
        return userLoginResult;
    }
}
